using System;
using UnityEngine;
using QFramework;
using UnityEngine.UI;

namespace XCZProject
{
	public partial class FloatingTextController : ViewController
	{
		void Start()
		{
			// Code Here
			FloatingText.Hide();
		}

		public static void Play(Vector2 position, string text,bool cirtical=false)
		{
			// 生成text，并设置父物体为FloatingTextController
			mDefault.FloatingText.InstantiateWithParent(mDefault.transform)
				.Position(position.x,position.y)
				.Self(f =>
				{
					var positionY = position.y;
					var textTrans = f.transform.Find("Text");
					var textComp = textTrans.GetComponent<Text>();
					textComp.text = text;

					// 如果暴击，则飘字颜色为红色
					if (cirtical)
					{
						textComp.color=Color.red;
					}

					// ActionKit.Delay(0.5f, () =>
					// {
					// 	textTrans.DestroyGameObjGracefully();
					// }).Start(textComp);

					// 掉血的移动动画
					ActionKit.Sequence()
						.Lerp(0, 0.5f, 0.5f, p =>
						{
							f.PositionY(positionY + p*0.25f);
							textComp.LocalScaleX(Mathf.Clamp01(p * 4));
							textComp.LocalScaleY(Mathf.Clamp01(p * 4));
						}, () =>
						{
							textTrans.DestroyGameObjGracefully();
						}).Start(textComp);
				}).Show();
		}

		private static FloatingTextController mDefault;

		private void Awake()
		{
			mDefault = this;
		}

		private void OnDestroy()
		{
			mDefault = null;
		}
	}
}
